package dbTools.XML;

import java.io.FileOutputStream;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;

import dbTools.util.FileUtil;

public class CommonXmlCreator {

	public static void createXML(Document doc,String path,String fileName)
	{
		path=path.endsWith("\\")?path:path+"\\";
		 try {
             //注意这里使用的是FileOutputStream，而不是FileWriter,因为涉及到编码问题
             //详细见“dom4j编码问题解决”篇。
			 
			 
			 //OutputFormat format=new OutputFormat("    ",true,"utf-8");//设置输出格式为空两格，换行，编码为utf-8
//			 
			 OutputFormat format =OutputFormat.createPrettyPrint();
			 format.setIndent(true);
			 format.setNewlines(true);
			 format.setEncoding("utf-8");
			 
			 FileUtil.makePath(path);

             org.dom4j.io.XMLWriter xmlWriter = new org.dom4j.io.XMLWriter(
                     new FileOutputStream(path+fileName),format);

             xmlWriter.write(doc);

             xmlWriter.close();

         }

         catch (Exception e) {

             System.out.println(e);

         }
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		 Document doc = org.dom4j.DocumentHelper.createDocument();
		 doc.addDocType("hibernate-mapping",
				 "-//Hibernate/Hibernate Mapping DTD 3.0//EN",
				 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd");
         Element root = doc.addElement("book");
         root.addComment("Comment Test!!!");

         root.addAttribute("name", "我的图书");

 

         Element childTmp;

         childTmp = root.addElement("price");

         childTmp.setText("21.22");

 

         Element writer = root.addElement("author");

         writer.setText("いつでも！");

         writer.addAttribute("ID", "001");

         createXML(doc,"E:\\" , "book.xml");
	}

}
